package com.gome.ss.core.schedule;

import java.util.HashMap;
import java.util.Map;

import com.gome.ss.core.plugin.PluginManager;

public class ScheduleManager {
	private static ScheduleManager instance = null;
	private static UpdatePluginStatuesJob updatePluginStatuesJob = new UpdatePluginStatuesJob();
	private Map<String, ScheduleCore> scheduleCoreMap = new HashMap<String, ScheduleCore>();
	
	private ScheduleManager() {
	}
	
	public static ScheduleManager getInstance() {
		if (instance == null) {
			synchronized (PluginManager.class) {
				 if ( instance == null ) {
					 instance = new ScheduleManager();
					 startJobjs();
				 }
			 }
		}
		
		return instance;
	}
	
	private static void startJobjs() {
		try {
			QuartzManager.addJob(updatePluginStatuesJob.getJobName(), updatePluginStatuesJob.getJob(), updatePluginStatuesJob.getTrigger());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ScheduleCore createScheduleCore(String name, int threadCount, int scheduleInterval) {
		if (scheduleCoreMap.containsKey(name))
			scheduleCoreMap.remove(name);
		
		ScheduleCore sc = new ScheduleCore();
		sc.init(name, threadCount, scheduleInterval);
		scheduleCoreMap.put(name, sc);
		return sc;
	}
	
	public ScheduleCore getScheduleCore(String name) {
		if (scheduleCoreMap.containsKey(name))
			return scheduleCoreMap.get(name);
		return null;
	}
	
	public void removeScheduleCore(String name) {
		if (scheduleCoreMap.containsKey(name)) {
			scheduleCoreMap.remove(name);
		}
	}
}
